VFG.DrawPolyline=function(viewer,option){
	this.viewer=viewer;
	this.option=option;
	this.id=option.id;
	this.thing=option.thing;
	this.position=null;
	this.tip=new VFG.Tip(viewer);
	this.enity;
	this.positions=[];
	this.init();
}

VFG.DrawPolyline.prototype.init=function() {
	let _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	this.handler.setInputAction(function (movement) {
		let cartesian3=VFG.Util.getScreenToC3 (_this.viewer, movement.position, _this.enity) 
		if(Cesium.defined(cartesian3)){
			if (_this.positions.length == 0) {
	        	_this.positions.push(cartesian3.clone());
	        }
			_this.positions.push(cartesian3);
		}
    },Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function (movement) {
    	if (_this.LEFT_CLICK_TIMES == 0){
    		_this.tip.showAt(movement.endPosition,"单击开始绘制，右键退出");
    	}else{
    		_this.tip.showAt(movement.endPosition,"绘制中，右键退出");
    	}
    	if(_this.positions.length >= 2){
			if(!_this.entity){
	            _this.entity= new VFG.EPolyline(_this.viewer,{
	            	id:VFG.Util.getUuid(),
	            	name:'',
	            	code:'',
	            	thing : {
	            	   positions :_this.positions,
	        	       materialType:_this.thing.materialType
	        	    }
	           });
			}else{
				let cartesian3=VFG.Util.getScreenToC3 (_this.viewer,movement.endPosition, _this.enity) 
		        if(Cesium.defined(cartesian3)){
		        	_this.positions.pop();
		            _this.positions.push(cartesian3);
		            _this.entity.updatePositions(_this.positions,true);
		        }
			}
		}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this.handler.setInputAction(function (movement) {
		if(_this.positions.length>=2 && _this.success){
			_this.success(_this.positions);
		}
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},

VFG.DrawPolyline.prototype.addListener=function(type,callback){
	if('success'==type){
		this.success=callback;
	}
	if('error'==type){
		this.error=callback;
	}
	if('finish'==type){
		this.finish=callback;
	}
}

VFG.DrawPolyline.prototype.removeListener=function(type){
	if('success'==type){
		this.success=null;
	}
	if('error'==type){
		this.error=null;
	}
	if('finish'==type){
		this.finish=null;
	}
}

VFG.DrawPolyline.prototype.destroy = function() {
	if(this.finish){
		this.finish();
	}
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	if(this.entity){
		this.entity.destroy();
	}
	delete this.entity;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
